const express = require('express');
const axios = require('axios');
const cors = require('cors');

const app = express();
const PORT = 3000;

// 替换为你的 DeepSeek API Key
const DEEPSEEK_API_KEY = 'sk-023ae10b2a5841d7a3ff0ebb20d44248';

app.use(cors()); // 允许跨域请求
app.use(express.json()); // 解析 JSON 请求体

// DeepSeek 代理接口
app.post('/api/ask-deepseek', async (req, res) => {
    try {
        const userMessage = req.body.message;

        // 调用 DeepSeek API（假设它的 API 和 OpenAI 类似）
        const response = await axios.post(
            'https://api.deepseek.com/v1/chat/completions',
            {
                model: 'deepseek-chat',
                messages: [{ role: 'user', content: userMessage }],
            },
            {
                headers: {
                    'Authorization': `Bearer ${DEEPSEEK_API_KEY}`,
                    'Content-Type': 'application/json',
                },
            }
        );

        const reply = response.data.choices[0].message.content;
        res.json({ reply });
    } catch (error) {
        console.error('DeepSeek API Error:', error.response?.data || error.message);
        res.status(500).json({ error: 'Failed to get response from DeepSeek' });
    }
});

app.listen(PORT, () => {
    console.log(`Server running on http://localhost:${PORT}`);
});